package com.wltt.product.frfront.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wltt.product.frfront.mapper.RobotLogMapper;
import com.wltt.product.frfront.model.ResultData;
import com.wltt.product.frfront.model.RspPage;
import com.wltt.product.frfront.model.po.RobotLogPO;
import com.wltt.product.frfront.model.vo.ListLogREQ;
import com.wltt.product.frfront.model.vo.ListLogRSP;
import com.wltt.product.frfront.service.base.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * @ClassName: RobotLogServiceImpl
 * @Description: 逻辑服务类<br />
 * 不做业务校验，参数校验统一放到前置的Service中
 * @Date: 2025-08-03 14:42:17
 * @Author yanggl
 */
@Service(value = "robotLogService")
public class RobotLogServiceImpl extends BaseServiceImpl<RobotLogPO, Long> implements RobotLogService {

    private RobotLogMapper robotLogMapper;

    //构造方法
    public RobotLogServiceImpl(RobotLogMapper robotLogMapper) {
        super(robotLogMapper);
        this.robotLogMapper = robotLogMapper;
    }

    /**
     * 机器人执行日志
     *
     * @param listLogReq
     * @return
     */
    @Override
    public ResultData listLog(ListLogREQ listLogReq) {
        String orderBy = " command_send_time desc ";
        Page page = PageHelper.startPage(listLogReq.getPageNo(), listLogReq.getPageSize(), orderBy);

        List<RobotLogPO> pos = robotLogMapper.queryAll();
        List<ListLogRSP> rsps = Optional.ofNullable(pos).orElse(new ArrayList<>())
                .stream().map(po -> {
                    ListLogRSP rsp = new ListLogRSP();
                    BeanUtils.copyProperties(po, rsp);
                    return rsp;
                }).collect(Collectors.toList());
        RspPage rspPage = new RspPage(listLogReq.getPageNo(), listLogReq.getPageSize(), rsps, page.getTotal());
        return ResultData.success(rspPage);
    }
}